package com.example.bookmall.controller.adminSystem;

import com.example.bookmall.entity.Book;
import com.example.bookmall.entity.Result;
import com.example.bookmall.entity.usercore.User;
import com.example.bookmall.service.BookService;
import com.example.bookmall.service.UserService;
import com.example.bookmall.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/books")
public class AdminBookController {

    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;

    //添加图书
    @PostMapping("/add")
    public Result addBook(@RequestBody Book book){

        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User currentuser = userService.findByUserName(userName);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }

        return bookService.addBook(book);
    }
    //修改图书
    @PutMapping("/modify/{bookId}")
    public Result modifyBook(@PathVariable Integer bookId, @RequestBody Book book)
    {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);

        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }

        return bookService.modifyBook(bookId, book);
    }
    //删除图书
    @DeleteMapping("/delete/{bookId}")
    public Result deleteBook(@PathVariable Integer bookId){

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);
        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return bookService.deleteBook(bookId)
                ? Result.success("图书删除成功")
                : Result.error("图书删除失败");
    }

    //查询图书
    @GetMapping("/list")
    public Result listBooks(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);
        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return Result.success(bookService.listBooks(page, pageSize));
    }


    //搜索图书根据图书名
    @GetMapping("/searchTitle")
    public Result searchTitle(@RequestParam String title,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);

        System.out.println("当前用户为：" + user.getUsername());

        if (user.getRole() == 1) {
            return Result.error("权限不足");
        }
        return Result.success(bookService.searchByTitle(title, page, pageSize));
    }
    //搜索图书根据作者
    @GetMapping("/searchAuthor")
    public Result searchAuthor(@RequestParam String author,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);
        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return Result.success(bookService.searchByAuthor(author, page, pageSize));
    }
    //搜索图书根据ISBN
    @GetMapping("/searchIsbn")
    public Result searchIsbn(@RequestParam String Isbn,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Map<String, Object> map = ThreadLocalUtil.get();

        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);
        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return Result.success(bookService.searchByIsbn(Isbn, page, pageSize));
    }


    //上架图书
    @PutMapping("/publish/{bookId}")
    public Result publishBook(@PathVariable Integer bookId) {

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);
        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }

        return bookService.publishBook(bookId)
                ? Result.success("图书上架成功")
                : Result.error("图书上架失败");
    }

    //下架图书
    @PutMapping("/unpublish/{bookId}")
    public Result unpublishBook(@PathVariable Integer bookId) {

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User currentuser = userService.findById(userId);
        if (currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }

        return bookService.unpublishBook(bookId)
                ? Result.success("图书下架成功")
                : Result.error("图书下架失败");
    }

}    